1 // -----------------------------------------------------------------------------------------------
2 // ------------------------------- radio 433MHz receiver and decoder -----------------------------
3 // -----------------------------------------------------------------------------------------------
5 extern void put_num( uint64_t num );
13 #define RX_TRESHOLD 5 // 16mHz crystall, see timer in UART routine
20 #define MIN_PACKET_LEN 40 // raw data, for filtering
21 #define MAX_PACKET_LEN 55 // raw data, for filtering
23 uint8_t RXBuff[ RX433_BUFF ];
26 uint8_t rx_done = IDLE;
28 uint8_t current_rx_bit = 0, prev_rx_bit = 0, keep_state = 0, current_state = 0;
30 // ------------------------------ tripe-bit decode -----------------------------------------------
32 uint8_t triple_bit_433( void ){
36 for ( a = 0; a < 12; a++ ){
38 switch( *(uint32_t *)(&RXBuff[a*4]) ){
40 case 0x00030201: RXBuff[ a ] = 'F'; break; // 1230
41 case 0x02010201: RXBuff[ a ] = '0'; break; // 1212
42 case 0x00030003: RXBuff[ a ] = '1'; break; // 3030
43 case 0x02010003: RXBuff[ a ] = 'X'; break; // 3012
45 default : return 0; break; // wrong sequence
52 // ------------------------------- manchester decode ---------------------------------------------
54 uint8_t manchester_433( void ){
59 for ( a = 0; a < RX433_BUFF; a++ ){
61 if ( RXBuff[a+1] == 5 ) break;
65 if ( RXBuff[a] == SHORT_LOW && RXBuff[a+1] == SHORT_HIGH ) { RXBuff[ newCnt ] = 1; a++; continue; }
66 if ( RXBuff[a] == SHORT_LOW && RXBuff[a+1] == LONG_HIGH ) { RXBuff[ newCnt ] = 1; continue; }
67 if ( RXBuff[a] == LONG_LOW && RXBuff[a+1] == SHORT_HIGH ) { RXBuff[ newCnt ] = 1; a++; continue; }
68 if ( RXBuff[a] == LONG_LOW && RXBuff[a+1] == LONG_HIGH ) { RXBuff[ newCnt ] = 1; continue; }
70 if ( RXBuff[a] == SHORT_HIGH && RXBuff[a+1] == SHORT_LOW ) { RXBuff[ newCnt ] = 0; a++; continue; }
71 if ( RXBuff[a] == SHORT_HIGH && RXBuff[a+1] == LONG_LOW ) { RXBuff[ newCnt ] = 0; continue; }
72 if ( RXBuff[a] == LONG_HIGH && RXBuff[a+1] == SHORT_LOW ) { RXBuff[ newCnt ] = 0; a++; continue; }
73 if ( RXBuff[a] == LONG_HIGH && RXBuff[a+1] == LONG_LOW ) { RXBuff[ newCnt ] = 0; continue; }
78 for ( a = newCnt; a < RX433_BUFF; a++ ) RXBuff[ a ] = 7;
84 // ------------------------------- capture raw data -------------------------------------------
85 // ------------------------------- for using inside the Timer ISR -----------------------------
87 void read433_handler( void ){
89 // current_rx_bit = (PINB & 0b00000100) >> 2;
90 current_rx_bit = (PINA & (1 << RX433)) >> RX433;
92 if (prev_rx_bit == current_rx_bit) {
94 if ( current_rx_bit == 0 && keep_state > RX_TRESHOLD * 4 && rx_done == READ && cnt > MIN_PACKET_LEN && cnt < MAX_PACKET_LEN ) { rx_done = DONE; RXBuff[ cnt ] = 5; } // end of packet
97 if ( prev_rx_bit == 0 && keep_state > RX_TRESHOLD * 4 && rx_done == IDLE ) { cnt = 0; rx_done = READ; } // begin of packet
100 if ( rx_done == READ ) {
102 if ( prev_rx_bit == 0 ){
103 if ( keep_state < RX_TRESHOLD ) current_state = SHORT_LOW; else current_state = LONG_LOW;
105 if ( keep_state < RX_TRESHOLD ) current_state = SHORT_HIGH; else current_state = LONG_HIGH;
108 if (cnt < RX433_BUFF ) {
109 if (cnt != 0) RXBuff[ cnt-1 ] = current_state;
112 rx_done = IDLE; // end of buffer
120 // treshold test : 16mHz, output: 13 11 3 3 12 10 4 3 11 11 4 2 12 2 12 3 11 11 4 2 12 11 3 3 11 11 4 2 12 11 3 3
122 if (cnt < RX433_BUFF ) {
123 RXBuff[ cnt++ ] = keep_state;
125 rx_done = DONE; // end
131 prev_rx_bit = current_rx_bit;